// 管理员模块 - 处理管理员相关操作
import baseAPI from './base.js'

// URLSearchParams的替代函数（兼容uni-app环境）
function buildQueryString(params) {
    const pairs = [];
    for (const key in params) {
        if (params[key] !== undefined && params[key] !== null && params[key] !== '') {
            pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(params[key]));
        }
    }
    return pairs.join('&');
}

class AdminAPI {
    // ==================== 用户管理 ====================

    // 获取用户列表
    async getAdminUserList(params = {}) {
        const queryParams = {};
        if (params.page) queryParams.page = params.page;
        if (params.pageSize) queryParams.page_size = params.pageSize;
        if (params.keyword) queryParams.keyword = params.keyword;
        if (params.role) queryParams.role = params.role;
        if (params.status) queryParams.status = params.status;
        
        const queryString = buildQueryString(queryParams);
        const url = queryString ? `/admin/users?${queryString}` : '/admin/users';
        
        return baseAPI.request(url, { method: 'GET' });
    }

    // 获取用户统计
    async getAdminUserStats() {
        return baseAPI.request('/admin/users/stats', { method: 'GET' });
    }

    // 更新用户角色
    async updateUserRole(userId, role) {
        return baseAPI.request(`/admin/users/${userId}/role`, {
            method: 'PUT',
            body: JSON.stringify({ role })
        });
    }

    // 封禁/解封用户
    async banUser(userId, status, reason = '') {
        return baseAPI.request(`/admin/users/${userId}/ban`, {
            method: 'PUT',
            body: JSON.stringify({ status, reason })
        });
    }

    // 获取用户角色信息
    async getUserRole(userId) {
        return baseAPI.request(`/admin/users/${userId}/role`, { method: 'GET' });
    }

    // 获取角色列表
    async getRoles() {
        return baseAPI.request('/admin/roles', { method: 'GET' });
    }

    // ==================== 公告管理 ====================

    // 获取公告列表
    async getAdminAnnouncementList(params = {}) {
        const queryParams = {};
        if (params.page) queryParams.page = params.page;
        if (params.pageSize) queryParams.page_size = params.pageSize;
        if (params.status) queryParams.status = params.status;
        if (params.type) queryParams.type = params.type;
        
        const queryString = buildQueryString(queryParams);
        const url = queryString ? `/admin/announcements?${queryString}` : '/admin/announcements';
        
        return baseAPI.request(url, { method: 'GET' });
    }

    // 创建公告
    async createAnnouncement(announcementData) {
        return baseAPI.request('/admin/announcements', {
            method: 'POST',
            body: JSON.stringify(announcementData)
        });
    }

    // 更新公告
    async updateAnnouncement(announcementId, updateData) {
        return baseAPI.request(`/admin/announcements/${announcementId}`, {
            method: 'PUT',
            body: JSON.stringify(updateData)
        });
    }

    // 发布公告
    async publishAnnouncement(announcementId) {
        return baseAPI.request(`/admin/announcements/${announcementId}/publish`, {
            method: 'POST'
        });
    }

    // 删除公告
    async deleteAnnouncement(announcementId) {
        return baseAPI.request(`/admin/announcements/${announcementId}`, {
            method: 'DELETE'
        });
    }

    // ==================== 系统管理 ====================

    // 获取系统日志
    async getSystemLogs(params = {}) {
        const queryParams = {};
        if (params.page) queryParams.page = params.page;
        if (params.pageSize) queryParams.page_size = params.pageSize;
        if (params.action) queryParams.action = params.action;
        if (params.userId) queryParams.user_id = params.userId;
        
        const queryString = buildQueryString(queryParams);
        const url = queryString ? `/admin/logs?${queryString}` : '/admin/logs';
        
        return baseAPI.request(url, { method: 'GET' });
    }

    // 获取系统信息
    async getSystemInfo() {
        return baseAPI.request('/admin/system', { method: 'GET' });
    }
}

// 创建全局实例
const adminAPI = new AdminAPI();

export default adminAPI; 